﻿PARAM([string]$RunStepType,[int]$PageSize,[int]$ImportLastCount)

$StartRow = $ImportLastCount
$EndRow = $ImportLastCount + $PageSize

$SQLServer = "SRV01"
$SQLDBName = "FIMSynchronization"
$SqlQuery = "Select * from (Select ROW_NUMBER() OVER (Order By object_id) as rowNum, * from dbo.mms_metaverse) sub where rowNum >= $StartRow and rowNum <= $EndRow"
 
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True"
 
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand
$SqlCmd.CommandText = $SqlQuery
$SqlCmd.Connection = $SqlConnection

$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $SqlCmd
 
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
 
$SqlConnection.Close()
 
clear
 
#$DataSet.Tables[0]

foreach($row in $DataSet.Tables[0].Rows)
{
    $csObject = @{}
    $csObject.Add(".objectType","Person")
    $csObject.Add(".ObjectModificationType","Add")
    $csObject.Add("FirstName",$row.firstName)
    $csObject.Add("LastName",$row.lastName)
    $csObject.Add("UniqueID",$row.accountName)
    $csObject.Add("Department",$row.department)
    try { $csObject.Add("employeeID",[int]$row.employeeID) } catch {}
    $csObject
}
